import java.util.HashMap;
import java.util.Vector;

public class Parser {
	
	private String str;
	private String strName;
	private String time;
	private Vector<String> params;
	private HashMap<String,String> paramMap;
	
	public Parser(String str) {
		this.str = str.substring(1,str.length() - 1);
		String temp = this.str.substring(0,this.str.indexOf('('));
		String[] items = temp.split(" ");
		this.strName = items[0];
		if (items.length > 1) {
			this.time = items[1];
		} else {
			this.time = "-1";
		}
		this.params = new Vector<String>();
		int open = 0;
		int openCounter = 0;
		int close = 0;
		int closeCounter = 0;
		for (int i = 0; i < this.str.length(); i++) {
			switch (this.str.charAt(i)) {
				case '(':
					if (openCounter == 0) {
						open = i;
					}
					openCounter++;
					break;
				case ')':
					closeCounter++;
					if (closeCounter == openCounter) {
						close = i;
						this.params.add(this.str.substring(open,close + 1));
						openCounter = 0;
						closeCounter = 0;
					}
					break;
				default:
					break;
			}
		}
		this.paramMap = new HashMap<String,String>();
	}
	
	public String getName() {
		return this.strName;
	}
	
	public String getTime() {
		return this.time;
	}
	
	public HashMap<String,String> getMap() {
		for (int i = 0; i < this.params.size(); i++) {
			String temp = this.params.elementAt(i);
			boolean flag = true;
			temp = temp.substring(1,temp.length() - 1);
			String name = "";
			int nextParamIndex = 0;
			if (temp.charAt(0) == '(') {
				if (temp.indexOf(')') != -1) {
					nextParamIndex = temp.indexOf(')');
					name = temp.substring(1,nextParamIndex);
					nextParamIndex++;
				} else {
					flag = false;
				}
				
			} else {
				nextParamIndex = temp.indexOf(' ');
				name = temp.substring(0,nextParamIndex);
				//nextParamIndex++;
			}
			if (flag) {
				String param = temp.substring(nextParamIndex + 1);
				this.paramMap.put(name, param);
				//System.out.println(name + " => " + param);
			}
		}
		return this.paramMap;
	}
	
	/*Test*/
	
	/*public static void main(String[] args) {
		String see = "(see 10 ((f c) 22.4 44 0 0) ((f c t) 48.4 5) ((f r t) 84.8 39) ((f p r t) 63.4 42) ((f t 0) 53 3) ((f t r 10) 57.4 12) ((g l) 34.1 31) ((f t r 20) 63.4 20) ((f t r 30) 70.1 26) ((f t r 40) 77.5 31) ((f t r 50) 85.6 36) ((f t l 10) 49.9 -8) ((f t l 20) 48.9 -19) ((f t l 30) 49.9 -31) ((f t l 40) 53 -42) ((f r t 30) 87.4 43) ((b) 22.2 44 0 0) ((p \"borregos\" 4) 22.2 7 0 0 -6 -6) ((p \"enemigo\") 54.6 30) ((p \"enemigo\") 44.7 25) ((p) 66.7 44) ((p) 54.6 41) ((p \"enemigo\") 54.6 31) ((l t) 46.5 71))";
		parser p = new parser(see);
		//String body = "(sense_body 5 (view_mode high normal) (stamina 8000 1 130600) (speed 0 -10) (head_angle 0) (kick 0) (dash 0) (turn 1) (say 0) (turn_neck 0) (catch 0) (move 1) (change_view 0) (arm (movable 0) (expires 0) (target 0 0) (count 0)) (focus (target none) (count 0)) (tackle (expires 0) (count 0)) (collision none) (foul  (charged 0) (card none)))";
		//parser p2 = new parser(body);
		System.out.println(p.getName());
		System.out.println(p.getTime());
		//System.out.println(p2.getName());
		//System.out.println(p2.getTime());
		HashMap<String,String> map = p.getMap();
		//HashMap<String,String> map2 = p2.getMap();
		//String type = "(player_type (id 0)(player_speed_max 1.05)(stamina_inc_max 45)(player_decay 0.4)(inertia_moment 5)(dash_power_rate 0.006)(player_size 0.3)(kickable_margin 0.7)(kick_rand 0.1)(extra_stamina 50)(effort_max 1)(effort_min 0.6)(kick_power_rate 0.027)(foul_detect_probability 0.5)(catchable_area_l_stretch 1))";
		//parser p3 = new parser(type);
		//System.out.println(p3.getName());
		//System.out.println(p3.getTime());
		//HashMap<String,String> map3 = p3.getMap();
		//System.out.println((String)map.get("new"));
	}*/
	
}
